package com.hkfrich.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkfrich.common.constant.Constants;
import com.hkfrich.common.domain.TreeSelect;
import com.hkfrich.common.utils.SecurityUtils;
import com.hkfrich.system.domain.SysRole;
import com.hkfrich.system.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import com.hkfrich.common.controller.BaseController;
import com.hkfrich.common.utils.ConvertUtils;
import com.hkfrich.common.utils.ResultUtils;
import com.hkfrich.system.domain.SysMenu;
//import com.hkfrich.common.core.page.TableDataInfo;

/**
 * @ClassName 菜单Controller
 * @Desscription
 * @Author _Heishuai
 * @Date 2022-09-27 21:46:25
 * @Version 1.0
 */
@Controller
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 通过条件查询单个菜单 不分页
     */
    @RequestMapping("/getOne")
    @ResponseBody
    public ResultUtils getOne(SysMenu sysMenu)
    {
        QueryWrapper<SysMenu> query = initBaseQueryWrapper(sysMenu);
        SysMenu one = sysMenuService.getOne(query);
        return ResultUtils.success(one,"菜单查询成功");
    }

    /**
     * 通过id查询单个菜单
     */
    @RequestMapping("/getOneById")
    @ResponseBody
    public ResultUtils getOneById(@RequestBody String paramsStr)
    {
        JSONObject params = getBodyParamsByStr(paramsStr);
        Integer id = params.getInteger("id");
        SysMenu one = sysMenuService.getById(id);
        return ResultUtils.success(one,"菜单查询成功");
    }

    /**
     * 查询菜单列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResultUtils list(@RequestBody SysMenu sysMenu)
    {
        List<SysMenu> menus = sysMenuService.selectMenuList(sysMenu, SecurityUtils.getUserId());
        return ResultUtils.success(menus,"菜单列表查询成功");
    }

    /**
     * 新增菜单
     */
    @PreAuthorize("hasAnyAuthority('system:menu:save')")
    @RequestMapping("/save")
    @ResponseBody
    public ResultUtils save(@RequestBody SysMenu sysMenu)
    {
        if (Constants.NOT_UNIQUE.equals(sysMenuService.checkMenuNameUnique(sysMenu)))
        {
            return ResultUtils.fail("新增菜单'" + sysMenu.getMenuName() + "'失败，菜单名称已存在");
        }
        sysMenu.setCreateBy(SecurityUtils.getUserId());
        sysMenuService.save(sysMenu);
        return ResultUtils.success("菜单保存成功");
    }

    /**
     * 修改菜单
     */
    @PreAuthorize("hasAnyAuthority('system:menu:update')")
    @RequestMapping("/updateById")
    @ResponseBody
    public ResultUtils updateById(@RequestBody SysMenu sysMenu)
    {
        if (Constants.NOT_UNIQUE.equals(sysMenuService.checkMenuNameUnique(sysMenu)))
        {
            return ResultUtils.fail("修改菜单'" + sysMenu.getMenuName() + "'失败，菜单名称已存在");
        }
        sysMenu.setUpdateBy(SecurityUtils.getUserId());
        sysMenuService.updateById(sysMenu);
        return ResultUtils.success("菜单修改成功");
    }

    /**
     * 删除菜单
     */
    @PreAuthorize("hasAnyAuthority('system:menu:remove')")
    @RequestMapping("/removeById")
    @ResponseBody
    public ResultUtils removeById(@RequestBody String paramsStr)
    {
        JSONObject params = getBodyParamsByStr(paramsStr);
        Long id = params.getLong("id");
        if (sysMenuService.hasChildByMenuId(id))
        {
            return ResultUtils.fail("存在子菜单,不允许删除");
        }
        if (sysMenuService.checkMenuExistRole(id))
        {
            return ResultUtils.fail("菜单已分配,不允许删除");
        }
        return ResultUtils.success(sysMenuService.removeById(id),"菜单删除成功");
    }
    /**
     * 修改启用状态
     */
    @RequestMapping("/changeMenuStatus")
    @ResponseBody
    public ResultUtils changeMenuStatus(@RequestBody SysMenu sysMenu) {
        return ResultUtils.success(sysMenuService.updateById(sysMenu),Constants.STATUS_DISABLE.equals(sysMenu.getStatus())?"停用成功":"启用成功");
    }
    /**
     * 批量删除菜单
     */
    @PreAuthorize("hasAnyAuthority('system:menu:remove')")
    @RequestMapping("/removeByIds")
    @ResponseBody
    public ResultUtils removeByIds(@RequestBody String paramsStr)
    {
        JSONObject params = getBodyParamsByStr(paramsStr);
        String ids = params.getString("ids");
        List<Long> list = ConvertUtils.toList(ids);
        return ResultUtils.success(sysMenuService.removeByIds(list),"批量删除菜单成功");
    }


    /**
     * 角色页面获取菜单下拉树列表
     */
    @RequestMapping("/treeselect")
    @ResponseBody
    public ResultUtils treeselect(@RequestBody SysMenu sysMenu)
    {
        List<SysMenu> menus = sysMenuService.selectMenuList(sysMenu, SecurityUtils.getUserId());
        List<TreeSelect> treeSelects = sysMenuService.buildMenuTreeSelect(menus);
        return ResultUtils.success(treeSelects,"获取树形菜单成功");
    }

    /**
     * 加载对应角色菜单列表树
     */
    @RequestMapping("/roleMenuTreeselect")
    @ResponseBody
    public ResultUtils roleMenuTreeselect(@RequestBody String paramsStr)
    {
        JSONObject params = getBodyParamsByStr(paramsStr);
        Long id = params.getLong("id");
        List<SysMenu> menus = sysMenuService.selectMenuList(SecurityUtils.getUserId());
        HashMap<String, Object> map = new HashMap<>();
        map.put("checkedKeys", sysMenuService.selectMenuListByRoleId(id));
        map.put("menus", sysMenuService.buildMenuTreeSelect(menus));
        return ResultUtils.success(map,"获取树形菜单成功");
    }
}
